package com.dys.teststreamapi11;

import com.dys.testlambda02.entity.Person;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @className: com.dys.teststreamapi11.StreamAPITest
 * @description: 对对流中的数据做分区和拼接
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/09/01 下午4:39
 */
public class StreamAPITest13 {
    /**
     * 这是一个main()方法,是应用程序的入口
     *
     * @param args 方法的形参列表,是一个字符串数组,包含了从命令行传递给程序的参数
     */
    public static void main(String[] args) {
        streamToSplice();
    }

    /**
     * 对流中的数据做分区
     */
    private static void streamToSubzone() {
        Map<Boolean, List<Person>> map = Stream.of(
                new Person("张三", 18),
                new Person("李四", 32),
                new Person("李思思", 13),
                new Person("张三", 20)).collect(Collectors.partitioningBy(p -> p.getAge() >= 18));
        map.forEach((k, v) -> System.out.println(k + "\t" + v));
    }

    /**
     * 对流中数据做拼接
     */
    private static void streamToSplice() {
        String names = Stream.of(
                new Person("张三", 18),
                new Person("李四", 32),
                new Person("李思思", 13),
                new Person("张三", 20)).map(Person::getName).collect(Collectors.joining());
        System.out.println(names);
        System.out.println("-------------------------");
        String name2 = Stream.of(
                new Person("张三", 18),
                new Person("李四", 32),
                new Person("李思思", 13),
                new Person("张三", 20)).map(Person::getName).collect(Collectors.joining("-"));
        System.out.println(name2);
        System.out.println("==========================");
        String name3 = Stream.of(
                new Person("张三", 18),
                new Person("李四", 32),
                new Person("李思思", 13),
                new Person("张三", 20)).map(Person::getName).collect(Collectors.joining("-", "[", "]"));
        System.out.println(name3);
    }

}
